﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using PB.FBMembership.DAL;
using System.Web.Security;

namespace PB.FBConnect
{
    internal class DBMapper
    {
        #region Constants
        private const string PROC_MemberGetInfo = "SP_MemberGetInfo";
        private const string PROC_MemberGetAll = "SP_MemberGetAll";
        private const string PROC_MemberCreate = "SP_MemberCreate";
        private const string PROC_MemberDelete = "SP_MemberDelete";
        private const string PROC_MemberUpdate = "SP_MemberUpdate";

        #endregion


        internal static CustomIdentity GetCustomIdentityFromDB(long uid)
        {
            CustomIdentity cUser = null;
            if (uid > 0)
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", uid);
                using (IDataReader reader = DBFacade.ExecuteDataReader(PROC_MemberGetInfo, parameters))
                {
                    if (reader != null && reader.Read())
                    {
                        cUser = GetCustomIdentityFromDataReader(reader);
                    }
                }
            }
            return cUser;
        }

        internal static void RegisterMember(CustomIdentity cUser)
        {
            if (cUser != null)
            {
                string picUrl = "default";
                if (!string.IsNullOrEmpty(cUser.Picture.Trim()))
                    picUrl = cUser.Picture;

                string dispName = "na";
                if (!string.IsNullOrEmpty(cUser.DisplayName))
                    dispName = cUser.DisplayName;
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", cUser.FacebookUserId);
                parameters.Add("pDisplayName", dispName);
                parameters.Add("pPicture", picUrl);
                parameters.Add("pIsLocked", cUser.IsLocked ? "Y" : "N");

                DBFacade.ExecuteNonQuery(PROC_MemberCreate, parameters);
            }
        }

        internal static bool UpdateUser(CustomIdentity cUser)
        {
            if (cUser != null)
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", cUser.FacebookUserId);
                parameters.Add("pDisplayName", cUser.DisplayName);
                parameters.Add("pPicture", cUser.Picture);
                parameters.Add("pIsLocked", cUser.IsLocked ? "Y" : "N");

                DBFacade.ExecuteNonQuery(PROC_MemberUpdate, parameters);
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static List<CustomIdentity> GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            List<CustomIdentity> cUserList = new List<CustomIdentity>();
            totalRecords = 0;
            CustomIdentity cUser = null;
            if (pageIndex >= 0 && pageSize >= 0)
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("PageIndex", pageIndex);
                parameters.Add("PageSize", pageSize);
                using (IDataReader reader = DBFacade.ExecuteDataReader(PROC_MemberGetAll, parameters))
                {
                    if (reader != null)
                    {
                        while(reader.Read())
                        {
                            cUser = GetCustomIdentityFromDataReader(reader);
                            cUserList.Add(cUser);
                        }
                    }
                }
            }
            totalRecords = cUserList.Count;
            return cUserList;
        }

        private static CustomIdentity GetCustomIdentityFromDataReader(IDataReader reader)
        {
            CustomIdentity cUser = null;
            long tempid = DataReaderHelper.GetData<Int64>(reader, "FBUserId");
            string dispName = DataReaderHelper.GetData<string>(reader, "DisplayName");
            DateTime stDate = DataReaderHelper.GetData<DateTime>(reader, "StartDate");
            string picUrl = DataReaderHelper.GetData<string>(reader, "Picture");
            string lockFlag = DataReaderHelper.GetData<string>(reader, "IsLocked");
            bool isLocked = lockFlag.ToUpper() == "Y" ? true : false;
            cUser = new CustomIdentity(tempid, dispName, picUrl, isLocked, stDate);
            return cUser;
        }
    }
}
